﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Utility;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class SystemLogProcessBll 
    {
        public static SystemLogCls[] ReadingSystemLog(
            string SiteId, 
            string FromDate, 
            string ToDate)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                "select * from TableLogAccess where AssetCode=@AssetCode ";
            DateTime dFromDate = FunctionUtilities.VNDateToDate(FromDate);
            DateTime dToDate = FunctionUtilities.VNDateToDate(ToDate);
            Query+=" and LogAccessDate between '"+dFromDate.ToString("MM/dd/yyyy 00:00")+"' and '"+dToDate.ToString("MM/dd/yyyy 23:59")+"' "+
                   "     order by LogAccessDate DESC";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new SystemLogCls[0];
            }
            SystemLogCls[] SystemLogs = SystemLogParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return SystemLogs;
        }


        public static UserNotLoginCls[] ReadingUserNotLogin(
            string SiteId,
            int    Month)
        {
            SiteParam OSiteParam = SiteParamUtility.CreateSiteParam(SiteId, WebConfig.XmlSiteConfigFile);
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            string Query =
                " select UserId=UserId,LoginName,FullName,LastAccessed=(select top 1 LogAccessDate from tableLogAccess where tableLogAccess.UserId=UserId order by LogAccessDate DESC ) into #temp_x from TableUser where AssetCode=@AssetCode; " +
                " select * from #temp_x where LastAccessed is null or  LastAccessed<dateadd(month, -" + Month + ", getdate()) order by LoginName; " +
                " drop table #temp_x; ";
            DataSet dsResult =
                    DBService.GetDataSet(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("AssetCode", AssetCode),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new UserNotLoginCls[0];
            }
            UserNotLoginCls[] UserNotLogin = UserNotLoginParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return UserNotLogin;
        }

        public static void AddSysLogProcess(
            string SiteId,
            string AssetCode,
            string Action,
            string ActionText,
            string UserId,
            string LoginName,
            string FullName)
        {
            string LogAccessId = System.Guid.NewGuid().ToString();
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.Insert(null, "TableLogAccess", 
                new DbParam[]
                {
                    new DbParam("LogAccessId",LogAccessId),
                    //new DbParam("LogAccessDate", System.DateTime.Now.ToString("yyyy/MM/dd"),
                         new DbParam("LogAccessDate", System.DateTime.Now,DbType.DateTime),
                    new DbParam("Action",Action),
                    new DbParam("ActionText",ActionText),
                    new DbParam("UserId",UserId),
                    new DbParam("LoginName",LoginName),
                    new DbParam("FullName",FullName),
                    new DbParam("AssetCode",AssetCode),
                });

        }


        public static void Delete(
            string SiteId,
            string[] Ids)
        {
            string DeleteItem = "(";
            for (int iIndex = 0; iIndex < Ids.Length; iIndex++)
            {
                DeleteItem += "'" + Ids[iIndex] + "'";
                if (iIndex < Ids.Length - 1)
                {
                    DeleteItem += ",";
                }
            }
            DeleteItem += ")";
            string DelQuery = "Delete from TableLogAccess where LogAccessId in " + DeleteItem;
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DBService.ExecuteNonQuery(null, DelQuery, null);
        }
    }
}
